using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.FacManage.Dto;
using ZR.Model.FacManage;
using ZR.Service.FacManage.IFacManageService;
using Infrastructure;

namespace ZR.Service.FacManage
{
    /// <summary>
    /// 车间管理Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IFacWorkshopService), ServiceLifetime = LifeTime.Transient)]
    public class FacWorkshopService : BaseService<FacWorkshop>, IFacWorkshopService
    {
        /// <summary>
        /// 查询车间管理列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<FacWorkshopDto> GetPageList(FacWorkshopQueryDto parm)
        {
            var predicate = QueryExp(parm);
            var response = new PagedInfo<FacWorkshopDto>();
            var total = 0;
            response.PageSize = parm.PageSize;
            response.PageIndex = parm.PageNum;
            response.Result = Queryable().Where(predicate.ToExpression())
                .LeftJoin<FacFactory>((wo, tory) => wo.FactoryId == tory.FactoryId)
                .OrderByPropertyNameIF(parm.Sort.IsNotEmpty(), parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc)
                .Select((wo, tory) => new FacWorkshopDto
                {
                    WorkshopId = wo.WorkshopId,
                    WorkshopCode = wo.WorkshopCode,
                    WorkshopName = wo.WorkshopName,
                    Remark = wo.Remark,
                    CreateBy = wo.CreateBy,
                    CreateTime = wo.CreateTime,
                    UpdateBy = wo.UpdateBy,
                    UpdateTime = wo.UpdateTime,
                    FactoryId = wo.FactoryId,
                    FactoryInfo = tory.FactoryCode + "  " + tory.FactoryName,
                }).ToPageList(parm.PageNum, parm.PageSize, ref total);
            response.TotalNum = total;
            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="WorkshopId"></param>
        /// <returns></returns>
        public FacWorkshop GetInfo(string WorkshopId)
        {
            var response = Queryable()
                .Where(x => x.WorkshopId == WorkshopId)
                .First();

            return response;
        }

        /// <summary>
        /// 获取车间树信息
        /// </summary>
        /// <param name="disabledFactory">禁用工厂选择</param>
        /// <returns></returns>
        public List<TreeSelectInfo> GetTreeList(bool disabledFactory)
        {
            List<TreeSelectInfo> trees = new List<TreeSelectInfo>();
            //获取工厂信息
            List<FacFactory> factoryList = this.Context.Queryable<FacFactory>().ToList();
            //获取车间信息
            List<FacWorkshop> workshopList = this.Context.Queryable<FacWorkshop>().ToList();
            foreach (FacFactory fac in factoryList)
            {
                TreeSelectInfo tree = new TreeSelectInfo();
                tree.value = fac.FactoryId;
                tree.label = fac.FactoryCode + " " + fac.FactoryName;
                tree.disabled = disabledFactory;
                foreach (FacWorkshop wo in workshopList.Where(it => it.FactoryId == fac.FactoryId).ToList())
                {
                    tree.children.Add(new TreeSelectInfo()
                    {
                        value = wo.WorkshopId,
                        label = wo.WorkshopCode + " " + wo.WorkshopName
                    });
                }
                trees.Add(tree);
            }
            return trees;
        }

        /// <summary>
        /// 添加车间管理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public FacWorkshop AddFacWorkshop(FacWorkshop model)
        {
            if (this.Any(it => it.WorkshopCode == model.WorkshopCode))
            {
                throw new CustomException($"系统中已包含{model.WorkshopCode}编码信息");
            }
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改车间管理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateFacWorkshop(FacWorkshop model)
        {
            if (this.Any(it => it.WorkshopCode == model.WorkshopCode && it.WorkshopId != model.WorkshopId))
            {
                throw new CustomException($"系统中已包含{model.WorkshopCode}编码信息");
            }
            return Update(model, true);
        }

        /// <summary>
        /// 删除车间信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteFacWorkshop(string[] ids)
        {
            if (this.Context.Queryable<FacLine>().Any(it => ids.Contains(it.WorkshopId)))
            {
                throw new CustomException($"系统中已经存在此车间的产线信息，不能删除");
            }
            return Delete(ids);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<FacWorkshop> QueryExp(FacWorkshopQueryDto parm)
        {
            var predicate = Expressionable.Create<FacWorkshop>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.WorkshopCode), it => it.WorkshopCode == parm.WorkshopCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.WorkshopName), it => it.WorkshopName.Contains(parm.WorkshopName));
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.FactoryId), it => it.FactoryId == parm.FactoryId);
            return predicate;
        }
    }
}